/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package maincz.tests.webapp.security;

import java.util.logging.Logger;
import maincz.tests.webapp.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 *
 * @author Main
 */
@Service
public class DBUserDetailsService implements UserDetailsService {

    @Autowired
    private IUserService userService;
    
    protected static final Logger log = Logger.getLogger(DBUserDetailsService.class.getName());
    
    public DBUserDetailsService() {
        super();
    }

    @Override
    public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException, DataAccessException {      
        log.info("loading user " + username);
        UserDetails userDetails = this.userService.getUser(username);
        log.info("Roles: ");
        for (GrantedAuthority auth: userDetails.getAuthorities()) {
            log.info(auth.getAuthority());
        }        
        return userDetails;
    }
}
